package com.ruyuan.eshop.common.service.trade.order.impl;

import com.ruyuan.eshop.common.framework.cache.Cache;
import com.ruyuan.eshop.common.framework.context.user.UserContext;
import com.ruyuan.eshop.common.framework.security.model.Buyer;
import com.ruyuan.eshop.common.model.base.CachePrefix;
import com.ruyuan.eshop.common.model.member.dos.MemberAddress;
import com.ruyuan.eshop.common.model.member.dos.ReceiptHistory;
import com.ruyuan.eshop.common.model.trade.order.enums.PaymentTypeEnum;
import com.ruyuan.eshop.common.model.trade.order.support.CheckoutParamName;
import com.ruyuan.eshop.common.model.trade.order.vo.CheckoutParamVO;
import com.ruyuan.eshop.common.service.trade.order.CheckoutParamService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 结算参数 业务层实现类
 *
 */
@Service
public class CheckoutParamServiceImpl implements CheckoutParamService {

    @Autowired
    private Cache cache;

    private MemberAddress createMemberAddress() {
        // mock一个收获地址信息
        MemberAddress memberAddress = new MemberAddress();
        memberAddress.setAddrId(27L);
        memberAddress.setMemberId(2L);
        memberAddress.setName("测试");
        memberAddress.setCountry("中国");
        memberAddress.setProvinceId(2L);
        memberAddress.setCityId(2813L);
        memberAddress.setCountyId(51976L);
        memberAddress.setTownId(null);
        memberAddress.setCounty("城区");
        memberAddress.setCity("徐汇区");
        memberAddress.setProvince("上海");
        memberAddress.setTown("");
        memberAddress.setAddr("测试地址");
        memberAddress.setTel("");
        memberAddress.setMobile("13333333333");
        memberAddress.setDefAddr(1);
        memberAddress.setShipAddressName("家");
        return memberAddress;
    }

    /**
     * 获取订单的创建参数<br>
     * 如果没有设置过参数，则用默认
     * @return 结算参数
     */
    @Override
    public CheckoutParamVO getParam() {
        //由Reids缓存中读取出结算参数
        CheckoutParamVO param = this.read();

        //如果缓存中没有 new一个，并赋给默认值
        if (param == null) {

            param = new CheckoutParamVO();
            Buyer buyer = UserContext.getBuyer();

            MemberAddress address = createMemberAddress();
            Long addrId = 0L;
            if (address != null) {
                addrId = address.getAddrId();
            }
            //默认配送地址
            param.setAddressId(addrId);

            //默认支付方式
            param.setPaymentType(PaymentTypeEnum.defaultType());

            //默认不需要发票
            ReceiptHistory receipt = new ReceiptHistory();
            param.setReceipt(receipt);

            //默认时间
            param.setReceiveTime("任意时间");

            //将结算参数写入缓存
            this.write(param);
        }

        return param;
    }

    /**
     * 设置收货地址id
     * @param addressId 收货地址id
     */
    @Override
    public boolean setAddressId(Long addressId) {
        // 保存收获地址到缓存中 TODO

        return true;
    }

    /**
     * 设置支付方式
     * @param paymentTypeEnum 支付方式{@link PaymentTypeEnum}
     */
    @Override
    public void setPaymentType(PaymentTypeEnum paymentTypeEnum) {
        this.cache.putHash(getRedisKey(), CheckoutParamName.PAYMENT_TYPE, paymentTypeEnum);
    }

    /**
     * 设置发票
     * @param receipt 发票信息 {@link  ReceiptHistory }
     */
    @Override
    public void setReceipt(ReceiptHistory receipt) {
        this.cache.putHash(getRedisKey(), CheckoutParamName.RECEIPT, receipt);
    }

    /**
     * 设置送货时间
     * @param receiveTime 送货时间
     */
    @Override
    public void setReceiveTime(String receiveTime) {
        this.cache.putHash(getRedisKey(), CheckoutParamName.RECEIVE_TIME, receiveTime);
    }

    /**
     * 设置订单备注
     * @param remark 订单备注
     */
    @Override
    public void setRemark(String remark) {
        this.cache.putHash(getRedisKey(), CheckoutParamName.REMARK, remark);
    }

    /**
     * 设置订单来源
     * @param clientType 客户端来源
     */
    @Override
    public void setClientType(String clientType) {
        this.cache.putHash(getRedisKey(), CheckoutParamName.CLIENT_TYPE, clientType);
    }

    /**
     * 取消发票
     */
    @Override
    public void deleteReceipt() {
        this.cache.putHash(getRedisKey(), CheckoutParamName.RECEIPT, null);
    }

    /**
     * 批量设置所有参数
     * @param paramVO 结算参数 {@link CheckoutParamVO}
     */
    @Override
    public void setAll(CheckoutParamVO paramVO) {
        this.write(paramVO);
    }

    /**
     * 检测是否支持货到付款
     * @param paymentTypeEnum 支付方式
     * @param addressId 会员收货地址id
     */
    @Override
    public void checkCod(PaymentTypeEnum paymentTypeEnum, Long addressId) {
        //如果支付方式不是货到付款，则不需要检测
        if(!PaymentTypeEnum.COD.equals(paymentTypeEnum)){
            return ;
        }

        //如果传入的会员收货地址id是null，则去缓存中读取会员收货地址
        if(addressId == null){
            CheckoutParamVO paramVO = this.getParam();
            addressId = paramVO.getAddressId();
        }

        //获取会员地址
        MemberAddress memberAddress = createMemberAddress();

        if(memberAddress == null){
            return;
        }

        List<Long> addIds = new ArrayList<>();
        addIds.add(memberAddress.getProvinceId());
        addIds.add(memberAddress.getCityId());
        addIds.add(memberAddress.getCountyId());
        addIds.add(memberAddress.getTownId());


    }


    /**
     * 读取结算参数缓存Key
     * @return 结算参数缓存Key+会员id
     */
    private String getRedisKey() {
        Buyer buyer = UserContext.getBuyer();
        return CachePrefix.CHECKOUT_PARAM_ID_PREFIX.getPrefix() + buyer.getUid();
    }


    /**
     * 将结算参数写入缓存
     * @param paramVO 结算参数
     */
    private void write(CheckoutParamVO paramVO) {
        String redisKey = getRedisKey();
        Map map = new HashMap<>(4);

        //设置收货地址
        if (paramVO.getAddressId() != null) {
            map.put(CheckoutParamName.ADDRESS_ID, paramVO.getAddressId());
        }
        //设置配送时间
        if (paramVO.getReceiveTime() != null) {
            map.put(CheckoutParamName.RECEIVE_TIME, paramVO.getReceiveTime());
        }
        //设置支付类型
        if (paramVO.getPaymentType() != null) {
            map.put(CheckoutParamName.PAYMENT_TYPE, paramVO.getPaymentType());
        }
        //设置发票
        if (paramVO.getReceipt() != null) {
            map.put(CheckoutParamName.RECEIPT, paramVO.getReceipt());
        }
        //设置备注
        if (paramVO.getRemark() != null) {
            map.put(CheckoutParamName.REMARK, paramVO.getRemark());
        }
        //设置客户端类型
        if (paramVO.getClientType() != null) {
            map.put(CheckoutParamName.CLIENT_TYPE, paramVO.getClientType());
        }

        this.cache.putAllHash(redisKey, map);
    }


    /**
     * 由Reids中读取出结算参数
     */
    private CheckoutParamVO read() {
        return null;
    }

}
